package com.zongxingh.falcon.logger.writer;

import com.google.gson.Gson;
import com.zongxingh.falcon.logger.config.LoggerProperties;
import com.zongxingh.falcon.logger.constant.LoggerGroup;
import com.zongxingh.falcon.logger.constant.LoggerLevel;
import com.zongxingh.falcon.logger.common.SimpleLog;
import com.zongxingh.falcon.logger.constant.LoggerTopic;
import com.zongxingh.falcon.mq.config.MqProperties;
import com.zongxingh.falcon.mq.producer.MQProducerFactory;
import com.zongxingh.falcon.mq.producer.MqProducer;
import com.zongxingh.falcon.mq.utils.ExceptionTools;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Introduction:  日志发送端抽象类，所有的日志发送端都该继承这个类
 * Package Name:  com.zongxingh.falcon.logger.writer
 * Project Name:  falcon-logger
 * Author:  zongxingh
 * Email： zongxingh@163.com
 * Tel: 157*****778
 * Create Time:  2020/10/25 16:20
 */
public abstract class AbstractLogWriter {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLogWriter.class);

    private LoggerLevel allowLevel;

    private Class<?> clazz;

    private LoggerProperties loggerProperties;

    private MqProperties mqProperties;

    private MqProducer mqProducer;

    protected AbstractLogWriter() {
    }

    protected AbstractLogWriter(Class<?> clazz,MqProperties mqProperties,LoggerProperties loggerProperties) {
        this.loggerProperties = loggerProperties;
        this.mqProperties = mqProperties;
        this.clazz = clazz;
        // 初始化用户设置的log输出级别
        String setedLevel = loggerProperties.getLevel();
        for (LoggerLevel level : LoggerLevel.values()){
            if (level.toString().equalsIgnoreCase(setedLevel)){
                allowLevel = level;
                break;
            }
        }
        this.mqProducer = MQProducerFactory.createProducer(LoggerGroup.LOG_MSG_GROUP, this.mqProperties);

    }

    public LoggerLevel getAllowLevel() {
        return allowLevel;
    }

    public void setAllowLevel(LoggerLevel allowLevel) {
        this.allowLevel = allowLevel;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }

    public LoggerProperties getLoggerProperties() {
        return loggerProperties;
    }

    public void setLoggerProperties(LoggerProperties loggerProperties) {
        this.loggerProperties = loggerProperties;
    }

    public MqProperties getMqProperties() {
        return mqProperties;
    }

    public void setMqProperties(MqProperties mqProperties) {
        this.mqProperties = mqProperties;
    }

    public MqProducer getMqProducer() {
        return mqProducer;
    }

    protected boolean sendLogMsg(SimpleLog log) {
        try {
            Gson gson = new Gson();
            return this.mqProducer.sedMessage(log.getTopic(),  gson.toJson(log));
        } catch (InterruptedException | RemotingException | MQClientException | MQBrokerException e) {
            LOGGER.error("向消息队列发送日志出现异常，异常信息：\n {}", ExceptionTools.getStackTraceAsString(e));
            return false;
        }
    }

    public abstract void trace(String logText);

    public abstract void debug(String logText);

    public abstract void info(String logText);

    public abstract void warn(String logText);

    public abstract void error(String logText);

}
